import vue from "@vitejs/plugin-vue";
import path from "path";
import { defineConfig, loadEnv } from "vite";
import WindiCSS from "vite-plugin-windicss";
import VueDevTools from "vite-plugin-vue-devtools";

// https://vitejs.dev/config/
export default ({ mode }) =>
  defineConfig({
    build: {
      target: "esnext",
      rollupOptions: {
        output: {
          // 在这里修改静态资源路径
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
        },
      },
    },
    base: "./",
    minify: true, // 是否压缩代码
    sourceMap: true, // 是否生成sourceMap
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
    server: {
      host: "0.0.0.0",
      cors: true,
      port: 8080,
      open: false, //自动打开
      proxy: {
        "/api": {
          target: loadEnv(mode, process.cwd()).VITE_APP_BASEURL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
    esbuild: {
      pure: ["console.log"], // 删除 console.log
      drop: ["debugger"], // 删除 debugger
    },
    plugins: [vue(), WindiCSS(), VueDevTools()],
  });
